﻿using System;
using System.Windows;
using System.Windows.Input;

namespace Alvin.Window.Utils.TouchView.Login
{
    /// <summary>
    /// UserLoginDialog.xaml 的交互逻辑
    /// </summary>
    public partial class UserLoginDialog : System.Windows.Window
    {
        public UserLoginDialog()
        {
            InitializeComponent();
        }

        private void usernameTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.passwordBox.Focus();
            }
        }

        private void passwordBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.Login();
            }
        }

        /// <summary>
        /// 登陆
        /// </summary>
        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            this.Login();
        }

        /// <summary>
        /// 退出
        /// </summary>
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Exit();
        }

        bool Login()
        {
            String userName = usernameTextBox.Text;
            String password = passwordBox.Password;
            int errorState = 0;
            bool isLoginSuccess = Hepler.UserLoginHepler.Login(userName, password, out errorState);

            if (isLoginSuccess)
            {
                PPLoggerUtils.LogPCClient(PPLogOperationType.Login, "登录");
                this.DialogResult = true;
            }
            else
            {
                string errorTitle = "错误";
                string errorInfo = "用户名密码不匹配";
                switch (errorState)
                {
                    case 1:
                        {
                            errorTitle = "权限错误";
                            errorInfo = "用户没有收银权限";
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                MessageBox.Show(errorInfo, errorTitle, MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            return isLoginSuccess;
        }

        void Exit()
        {
            this.Close();
        }
    }
}
